<?php
require_once('config.inc.php');

page_open( array('sess' => 'MySession') );

page_close();

/**
 * Retourne un montant en euro, ou rien du tout
 */ 
function montant($montant)
{
  return $montant ? "$montant €" : '&nbsp;'; 
}

/**
 * NEW CATEGORIE
 */ 
function get_option_categorie($id, $nom)
{
  return <<<EOOC
<option value="$id">$nom</option>

<script type="text/javascript">
  Form.Element.setValue('select_categories', $id);
</script>
EOOC;

}
/**
 * ROW OPERATION
 */ 
function get_row_operation($id, $date, $credit, $debit, $cat, $infos, $id_cat)
{
  if ($credit > 0) 
  {
    $type='credit';
    $tab = split(' ', $credit);
    $montant = $tab[0];
  }else
  { 
    $type='debit';
    $tab = split(' ', $debit);
    $montant = $tab[0];
  }
   
  return <<<EORO
  <tr id="op_$id">
    <td class="date">$date</td>
    <td class="montant">$credit</td>
    <td class="montant">$debit</td>
    <td class="cat">$cat<div id="infos_$id">$infos</div></td>
    <td class="action">
    <a href="#" id="btn_edit_$id"><img src="img/edit.png" title="Editer l'opération"></a></td>
    <td class="action">
    <a href="#" id="btn_del_$id"><img src="img/drop.png" title="Supprimer l'opération"></a></td></tr>

<script type="text/javascript">
  window.Compte.look_op("op_$id", $id, $id_cat, "$date","$type","$montant","$infos");
</script>
EORO;
}

/**
 * get_row_compte_content
 */ 
function get_row_operation_content($id, $date, $credit, $debit, $cat, $infos, $id_cat)
{
  if ($credit > 0) 
  {
    $type='credit';
    $tab = split(' ', $credit);
    $montant = $tab[0];
    $credit .= ' €';
  }else
  { 
    $type='debit';
    $tab = split(' ', $debit);
    $montant = $tab[0];
    $debit .= ' €';
  }
  
  return <<<EORCC
    <td class="date">$date</td>
    <td class="montant">$credit</td>
    <td class="montant">$debit</td>
    <td class="cat">$cat<div id="infos_$id">$infos</div></td>
    <td class="action">
    <a href="#" id="btn_edit_$id"><img src="img/edit.png" title="Editer l'opération"></a></td>
    <td class="action">
    <a href="#" id="btn_del_$id"><img src="img/drop.png" title="Supprimer l'opération"></a></td>

<script type="text/javascript">
  window.Compte.look_op("op_$id", "$id", "$id_cat", "$date","$type","$montant","$infos");
</script>  

EORCC;
}

/** 
 * ROW BILAN CATEGORIE
 */ 
function get_row_categorie($cat, $montant)
{
  return <<<EORC
  <tr>
    <th>$cat</th>
    <td class="montant">$montant €</td></tr>
EORC;
}

/**
 * get_delete_code
 */ 
function get_delete_operation($id)
{
  return "window.Compte.delete_op($id);";
}


// ACTIONS
switch (rd_get('action'))
{
case 'load':
  /// LOAD
  $operations = Gestion::get_operations($sess_id_compte, $sess_date);
  foreach ($operations as $op)
  {
    $credit = montant($op['type'] == 'credit' ? $op['montant'] : false);
    $debit  = montant($op['type'] == 'debit'  ? $op['montant'] : false);
    print get_row_operation( $op['id']
                           , DateSql::Sql2Fr($op['date'])
                           , $credit
                           , $debit
                           , $op['cat']
                           , $op['infos'] ? addslashes($op['infos']) : '&nbsp;'
                           , $op['id_cat']
                           );
  }
  break;
  
case 'delete':
  $id = rd_get('id');
  if (Gestion::delete_operation($id))
  {
    header("Content-Type: application/javascript");
    print get_delete_operation($id);
  }
  break;  
  
case 'update_op':
  $id      = rd_get('id_op');
  $id_cat  = rd_get('id_cat');
  $infos   = rd_get('infos');
  $montant = rd_get('montant');  
  $type    = rd_get('type');
  $date    = DateSql::Fr2Sql(rd_get('date'));  
  
  Gestion::update_operation($id, $date, $montant, $id_cat, $infos);
  $info = Gestion::get_info_operation($id);
                            
  if ($type == 'credit')$credit=$info['montant'];
  if ($type == 'debit') $debit=$info['montant'];                      
  print get_row_operation_content($id, DateSql::Sql2Fr($date), $credit, $debit, $info['cat'], $info['infos'], $info['id_cat']);
  break;
  
 
case 'get_solde':
  /// GET SOLDE
  print Gestion::get_solde($sess_id_compte);
  break;
  
case 'get_list_categories':
  /// GET LISTE CATEGORIES
  $categories = Gestion::get_categories();
  foreach ($categories as $cat)
  {
    printf('<option value="%d">%s</option>', $cat['id'], $cat['nom']);
  }
  break;

case 'get_bilan_categories':
  /// GET BILAN CATEGORIES
  $categories = Gestion::get_categories();
  foreach ($categories as $cat)
  {
    print get_row_categorie($cat['nom'], Gestion::get_montant_categorie($sess_id_compte, $cat['id']));
  }
  break;

case 'new_cat':
  /// NEW CATEGORIE
  $nom = rd_get('nom');
  if ($nom)
  {
    $id  = Gestion::add_categorie($nom);
    if ($id)
    {
      print get_option_categorie($id, $nom);
    }
  }
  break;

case 'add_op' :
  // NEW OPERATION
  $date    = DateSql::Fr2Sql(rd_get('date'));
  $montant = rd_get('montant');
  $id_cat  = rd_get('id_cat');
  $debit   = rd_get('type') != 'credit';
  $infos   = rd_get('infos');
  $id_op   = Gestion::add_operation($sess_id_compte, $date, $montant, $id_cat, $debit, $infos);
  if ($id_op)
  {
    $credit = montant(rd_get('type') == 'credit' ? $montant : false);
    $debit  = montant(rd_get('type') == 'debit'  ? $montant : false);
    $categorie = Gestion::get_categorie($id_cat);
    print get_row_operation( $id_op
                           , DateSql::Sql2Fr($date)
                           , $credit
                           , $debit
                           , $categorie
                           , $infos ? $infos : '&nbsp;'
                           , $id_cat
                           );
  }
  break;
}
?>
